<template>
  <div class="student">
    
    <h3 class="StudentTitle">学员信息</h3>
    <el-button
      class="add"
      type="primary"
      @click="addRow"
      plain
    >添加</el-button>

    <el-table
      :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
      class="studentTable"
      :border="border"
      :header-cell-style="{textAlign: 'center'}"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column
        label="name"
        prop="name"
      >
      </el-table-column>
      <el-table-column
        label="sex"
        prop="sex"
      >
      </el-table-column>
      <el-table-column
        label="age"
        prop="age"
      >
      </el-table-column>
      <el-table-column
        label="mail"
        prop="mail"
      >
      </el-table-column>
      <el-table-column
        label="phone"
        prop="phone"
      >
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="editDialog(scope.$index)"
            ref="edit"
          >编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click.native.prevent="deleteRow(scope.$index, tableData)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 20, 40]"
      :page-size="pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="tableData.length"
      class="mypagination"
    >
    </el-pagination>

    <el-dialog
      title="个人信息"
      :visible.sync="dialogFormVisible"
      width="40%"
      :close-on-click-modal="closeOnClick"
    >
      <el-form
        :model="form"
        :rules="rules"
        ref="dialogForm"
      >
        <el-form-item
          label="姓名"
          :label-width="formLabelWidth"
          prop="name"
        >
          <el-input
            v-model="form.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item
          label="性别"
          :label-width="formLabelWidth"
          prop="sex"
        >
          <el-radio-group v-model="form.sex">
            <el-radio
              label="男"
              value="男"
            ></el-radio>
            <el-radio
              label="女"
              value="女"
            ></el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item
          label="年龄"
          :label-width="formLabelWidth"
          prop="age"
        >
          <el-input
            v-model="form.age"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="邮箱"
          :label-width="formLabelWidth"
          prop="mail"
        >
          <el-input
            v-model="form.mail"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="电话"
          :label-width="formLabelWidth"
          prop="phone"
        >
          <el-input
            v-model="form.phone"
            autocomplete="off"
          ></el-input>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="subData"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Student',
  data() {
    return {
      // 表格的数据
      tableData: [
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "男", age: 32, mail: "1222232@qq.com", phone: "2341241", pid: 0 },
        { name: "fff", sex: "女", age: 32, mail: "12222123232@qq.com", phone: "2341241", pid: 0 }
      ],
      
      dialogFormVisible: false,  // 控制模态框是否显示
      formLabelWidth: '80px',  // 模态框中form的label宽度
      border: true,  // 是否开启边框
      // 模态框中的数据
      form: {
        name: '',
        sex: '',
        age: '',
        mail: '',
        phone: '',
        pid: ''
      },
      isAdd: true,  // 判断是否是添加按钮开启的模态框
      editIndex: 0, // 点击修改按钮的那一行的索引
      closeOnClick: false, // 是否能通过点击模态框关闭模态框
      // 表单验证规则
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'change' }
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' }
        ],
        mail: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        phone: [
          { required: true, message: '请输入电话', trigger: 'blur' }
        ]
      },
      currentPage: 1, //初始页
      pagesize: 10,    //    每页的数据
    }
  },
  methods: {
    // pageSize 改变时会触发
    handleSizeChange(size) {
      this.pagesize = size;
      console.log(this.pagesize)  //每页下拉显示数据
    },

    // currentPage 改变时会触发
    handleCurrentChange(currentPage) {
      this.currentPage = currentPage;
      console.log(this.currentPage)  //点击第几页
    },

    // 通过点击修改触发模态框时的处理
    editDialog(index) {
      this.isAdd = false;
      this.editIndex = index;
      this.dialogFormVisible = true;
      // 将模态框中的数据修改为对应下标的表格数据
      this.form = Object.assign({}, this.tableData[index])
    },

    // 删除
    deleteRow(index, rows) {
      this.$confirm('确认删除？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        rows.splice(index, 1);
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });

    },

    // 添加时触发模态框的处理
    addRow() {
      this.dialogFormVisible = true;
      this.isAdd = true;
    },

    // 提交数据
    subData() {
      // 如果表单的验证规则通过，再执行接下来的操作
      this.$refs['dialogForm'].validate((valid) => {
        if (valid) {
          if (this.isAdd) {
            // 如果是添加，则直接在表格数据中添加
            this.tableData.push(this.form)
          } else {
            // 如果是修改，则通过Object.assign拷贝一份form中的数据，修改对应下标中的表格数据
            this.tableData[this.editIndex] = Object.assign({}, this.form)
          }
          this.dialogFormVisible = false;
          this.$message({
            message: '提交成功',
            type: 'success'
          });
        } else {
          this.$message.error('填写信息有问题哦');
          return false;
        }
      });

    }
  },
  watch: {
    // 监听模态框的关闭，如果关闭，则清空模态框中的数据
    dialogFormVisible() {
      if (this.dialogFormVisible == false) {
        this.form = {
          name: '',
          sex: '',
          age: '',
          mail: '',
          phone: '',
          pid: ''
        }
      }

    }
  }
}
</script>

<style scoped>
.student{
  width: 60%;
  margin: 60px auto 0 auto;
}
.StudentTitle {
  text-align: center;
  margin-bottom: 10px;
}
.studentTable{
  margin-bottom: 30px;
}
.add {
  position: relative;
  left: 70%;
  margin-bottom: 30px;
}
.mypagination{
  text-align: center;
  margin: 0 auto;
}
</style>